package com.ruoyi.develop.goodsReimbursementDetail.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.develop.goodsFee.domain.GoodsFee;
import com.ruoyi.develop.goodsFee.service.IGoodsFeeService;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.service.IGoodsOrderInService;
import com.ruoyi.develop.goodsReimbursement.domain.GoodsReimbursement;
import com.ruoyi.develop.goodsReimbursement.service.IGoodsReimbursementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.goodsReimbursementDetail.mapper.GoodsReimbursementDetailMapper;
import com.ruoyi.develop.goodsReimbursementDetail.domain.GoodsReimbursementDetail;
import com.ruoyi.develop.goodsReimbursementDetail.service.IGoodsReimbursementDetailService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 发放打款明细信息管理Service业务层处理
 *
 * @author qh
 * @date 2023-01-16
 */
@Service
public class GoodsReimbursementDetailServiceImpl implements IGoodsReimbursementDetailService {
    @Resource
    private GoodsReimbursementDetailMapper goodsReimbursementDetailMapper;
    @Autowired
    private IGoodsReimbursementService goodsReimbursementService;
    @Autowired
    private IGoodsFeeService feeService;
    @Autowired
    private IGoodsOrderInService orderInService;

    /**
     * 查询发放打款明细信息管理
     *
     * @param id 发放打款明细信息管理主键
     * @return 发放打款明细信息管理
     */
    @Override
    public GoodsReimbursementDetail selectById(String id) {
        return goodsReimbursementDetailMapper.selectById(id);
    }

    /**
     * 查询发放打款明细信息管理列表
     *
     * @param entity 发放打款明细信息管理
     * @return 发放打款明细信息管理
     */
    @Override
    public List<GoodsReimbursementDetail> selectList(GoodsReimbursementDetail entity) {
        return goodsReimbursementDetailMapper.selectList(entity);
    }

    /**
     * 新增发放打款明细信息管理
     *
     * @param entity 发放打款明细信息管理
     * @return 结果
     */
    @Override
    @Transactional(readOnly = false)
    public AjaxResult insert(GoodsReimbursementDetail entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());

        GoodsReimbursementDetail imburseDetailParam = new GoodsReimbursementDetail();
        imburseDetailParam.setReimburseId(entity.getReimburseId());
        List<GoodsReimbursementDetail> projectFeeInfoList = selectList(imburseDetailParam);
        BigDecimal allMoney = entity.getGrantMoney();
        for(GoodsReimbursementDetail imburseDetail : projectFeeInfoList){
            if (imburseDetail.getGrantMoney() != null) {
                allMoney = imburseDetail.getGrantMoney().add(allMoney);
            }
        }
        GoodsReimbursement imbursement = goodsReimbursementService.selectById(entity.getReimburseId());
        if(allMoney.compareTo(imbursement.getImburseMoney()) < 1){
            imbursement.setRemMoney(allMoney);
            imbursement.setRemRemain(imbursement.getImburseMoney().subtract(allMoney));
            GoodsFee feeParam = new GoodsFee();
            feeParam.setImburseId(imbursement.getId());
            GoodsOrderIn orderInParam = new GoodsOrderIn();
            orderInParam.setImburseId(imbursement.getId());
            if(imbursement.getRemRemain().compareTo(BigDecimal.ZERO) == 0){//已发放
                imbursement.setStatus("2");
                //根据报销imburse_id更新费用状态为 : yhx
                feeParam.setStatus("yhx");
                orderInParam.setReimburse("1");
            }else{//部分发放 bfhx
                feeParam.setStatus("bfhx");
                orderInParam.setReimburse("0");
            }
            orderInService.updateReimburseStatusByImburseId(orderInParam);
            feeService.updateFeeStatusByImburseId(feeParam);
            goodsReimbursementService.update(imbursement);
            goodsReimbursementDetailMapper.insert(entity);
            return AjaxResult.success("操作成功");
        }else{
            return AjaxResult.error("操作失败,发放金额超出报销金额");
        }
    }

    /**
     * 修改发放打款明细信息管理
     *
     * @param entity 发放打款明细信息管理
     * @return 结果
     */
    @Override
    public int update(GoodsReimbursementDetail entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return goodsReimbursementDetailMapper.update(entity);
    }

    /**
     * 批量删除发放打款明细信息管理
     *
     * @param ids 需要删除的发放打款明细信息管理主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return goodsReimbursementDetailMapper.deleteByIds(ids);
    }

    /**
     * 删除发放打款明细信息管理信息
     *
     * @param id 发放打款明细信息管理主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return goodsReimbursementDetailMapper.deleteById(id);
    }
}
